package org.xhnews.ui;

import java.util.Vector;

import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;

import org.xhnews.bo.NewsBo;
import org.xhnews.to.NewsChannel;

public class ChannelListField extends ListField implements ListFieldCallback,ShowWindow{
	private Vector _channellist;
	public void initialize() {	
		reload();
	}
	public ChannelListField() {
		_channellist = new Vector();
		setCallback(this);
		initialize();
	}
	public void reload() {
		_channellist = NewsBo.getAllEnableChannels();
		setSize(_channellist.size());
	}
	public void drawListRow(ListField list, Graphics g, int index, int y,
			int w) {
		String text = ((NewsChannel) _channellist.elementAt(index))
				.getTitle();
		g.drawText(text, 0, y, 0, w);
	}

	public Object get(ListField list, int index) {
		return _channellist.elementAt(index);
	}

	public int indexOfList(ListField list, String prefix, int string) {
		return _channellist.indexOf(prefix, string);
	}

	public int getPreferredWidth(ListField list) {
		return Display.getWidth();
	}
	private void navigationToNewsListScreen() {
		NewsChannel newschannel = (NewsChannel) _channellist.elementAt(getSelectedIndex());		
		NewsListScreen screen = new NewsListScreen();
		screen.initialize(newschannel);
		UiApplication.getUiApplication().pushScreen(screen);
	}

	protected boolean navigationClick(int status, int time) {
		navigationToNewsListScreen();
		return true;
	}

	protected boolean keyChar(char key, int status, int time) {
		if (key == Characters.ENTER) {
			navigationToNewsListScreen();
		}
		return true;
	}
}
